﻿// <copyright file="DataRequest.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-09-23</date>
// <summary>OpenSocial Data Reques</summary>

namespace opensocial
{
    using System.Diagnostics.CodeAnalysis;
    using System.Windows.Browser;
    using Synergist;

    /// <summary>
    /// Raw data received delegate
    /// </summary>
    /// <param name="dataResponse">the data response from JavaScript</param>
    public delegate void RawDataReceived(ScriptObject dataResponse);

    /// <summary>
    /// Data received delegate
    /// </summary>
    /// <param name="dataResponse">the data response</param>
    public delegate void DataReceived(DataResponse dataResponse);

    /// <summary>
    /// OpenSocial Data request
    /// </summary>
    public class DataRequest
    {
        /// <summary>
        /// Initializes a new instance of the DataRequest class.
        /// </summary>
        /// <param name="dataRequest">data request</param>
        internal DataRequest(ScriptObject dataRequest)
        {
            ScriptObject = dataRequest;
        }

        /// <summary>
        /// Gets or sets the underlying script object
        /// </summary>
        public ScriptObject ScriptObject { get; set; }

        /// <summary>
        /// Add an item to the data request
        /// </summary>
        /// <param name="request">an item to add to the data request</param>
        /// <remarks>Ignore SA1300</remarks>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "add", Justification = "Mirroring OpenSocial API.")]
        public void add(ScriptObject request)
        {
            ScriptObject.Invoke("add", request);
        }

        /// <summary>
        /// Add an item to the data request
        /// </summary>
        /// <param name="request">an item to add to the data request</param>
        /// <param name="optKey">the key to use when getting the query results</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "add", Justification = "Mirroring OpenSocial API.")]
        public void add(ScriptObject request, string optKey)
        {
            ScriptObject.Invoke("add", request, optKey);
        }

        /// <summary>
        /// Send the data request
        /// </summary>
        /// <param name="opt_callback">event callback</param>
        /// <remarks>Ignore SA1300</remarks>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "send", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "callback", Justification = "Mirroring OpenSocial API.")]
        public void send(DataReceived opt_callback)
        {
            RawDataReceived myDelegate = delegate(ScriptObject dataResponse)
            {
                opt_callback(new opensocial.DataResponse(dataResponse));
            };

            ScriptObject.Invoke("send", myDelegate);
        }

        #region IDataRequest Members

        /// <summary>
        /// new fetch people request
        /// </summary>
        /// <param name="idSpec">the id spec</param>
        /// <returns>the new request</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "new", Justification = "Mirroring OpenSocial API.")]
        public ScriptObject newFetchPersonRequest(string idSpec)
        {
            var request = ScriptObject.Invoke("newFetchPersonRequest", idSpec) as ScriptObject;

            return request;
        }

        /// <summary>
        /// new fetch people request
        /// </summary>
        /// <param name="idSpec">the id spec</param>
        /// <returns>the new request</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "new", Justification = "Mirroring OpenSocial API.")]
        public ScriptObject newFetchPeopleRequest(string idSpec)
        {
            var request = ScriptObject.Invoke("newFetchPeopleRequest", idSpec) as ScriptObject;

            return request;
        }

        /// <summary>
        /// create a new Fetch People Request
        /// </summary>
        /// <param name="idSpec">id specification</param>
        /// <param name="opt_params">optional parameters</param>
        /// <returns>a new fetch people request</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "params", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "params", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "new", Justification = "Mirroring OpenSocial API.")]
        public ScriptObject newFetchPeopleRequest(string idSpec, object opt_params)
        {
            var request = ScriptObject.Invoke("newFetchPeopleRequest", idSpec, opt_params) as ScriptObject;

            return request;
        }

        /// <summary>
        /// create a new Fetch People Request
        /// </summary>
        /// <param name="idSpec">id specification</param>
        /// <param name="opt_params">optional parameters</param>
        /// <returns>a new fetch people request</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "params", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "params", Justification = "Mirroring OpenSocial API."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "new", Justification = "Mirroring OpenSocial API.")]
        public ScriptObject newFetchPeopleRequest(opensocial.IdSpec idSpec, object opt_params)
        {
            var request = ScriptObject.Invoke("newFetchPeopleRequest", idSpec.ScriptObject, opt_params) as ScriptObject;

            return request;
        }

        /// <summary>
        /// Creates an item to request an update of an app field for the given person. When processed, does not return any data. 
        /// </summary>
        /// <param name="idSpec">The ID of the person to update; only the special VIEWER ID is currently allowed.</param>
        /// <param name="key">The name of the key. This may only contain alphanumeric (A-Za-z0-9) characters, underscore(_), dot(.) or dash(-).</param>
        /// <param name="value">The value, must be valid json</param>
        /// <returns>A request object</returns>
        /// <remarks>Uses the <a href="http://wiki.opensocial.org/index.php?title=The_Persistence_API">OpenSocial Persistance API</a></remarks>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "new", Justification = "Matching OpenSocial JavaScript API.")]
        public ScriptObject newUpdatePersonAppDataRequest(string idSpec, string key, string value)
        {
            var request = ScriptObject.Invoke("newUpdatePersonAppDataRequest", idSpec.Eval(), key, value) as ScriptObject;

            return request;
        }

        /// <summary>
        /// Fetch a person's application data
        /// </summary>
        /// <param name="idSpec">the person's id Specification</param>
        /// <param name="key">the data key</param>
        /// <param name="opt_params">optional parameters</param>
        /// <returns>a data request</returns>
        /// <remarks>Uses the <a target="_blank" href="http://wiki.opensocial.org/index.php?title=The_Persistence_API">OpenSocial Persistance API</a></remarks>
        [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores", Justification = "Matching OpenSocial JavaScript API."), SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "params", Justification = "Matching OpenSocial JavaScript API."), SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "params", Justification = "Matching OpenSocial JavaScript API."), SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "new", Justification = "Matching OpenSocial JavaScript API.")]
        public ScriptObject newFetchPersonAppDataRequest(IdSpec idSpec, string key, object opt_params)
        {
            var request = ScriptObject.Invoke("newFetchPersonAppDataRequest", idSpec.ScriptObject, key, opt_params) as ScriptObject;

            return request;
        }

        /// <summary>
        /// Removes a value from persistent applicaiton data
        /// </summary>
        /// <param name="idSpec">the user's Id</param>
        /// <param name="key">the key to remove</param>
        /// <returns>a data request</returns>
        /// <remarks>Uses the <a href="http://wiki.opensocial.org/index.php?title=The_Persistence_API">OpenSocial Persistance API</a></remarks>
        [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "new", Justification = "Matching OpenSocial JavaScript API.")]
        public ScriptObject newRemovePersonAppDataRequest(string idSpec, string key)
        {
            var request = ScriptObject.Invoke("newRemovePersonAppDataRequest", idSpec.Eval(), key) as ScriptObject;

            return request;
        }

        #endregion
    }
}
